package com.pes.arcww.dao;

import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.pes.arcww.form.Items;
import com.pes.arcww.form.Questions;
import com.pes.arcww.form.Topics;

@Repository
public class QuestionsDAOImpl implements QuestionsDAO{
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public void addQuestion(Questions question) {
		sessionFactory.getCurrentSession().save(question);
	}
	
	public void addItem(Integer que_id, Items item) {
		Questions question = getQuestion(que_id);
		item.setQuestion(question);
		sessionFactory.getCurrentSession().save(item);
	}
	public void addTopic(Integer que_id, Topics topic) {
		Questions question = getQuestion(que_id);
		question.setTopic(topic);
		updateQuestion(question);
	}

	@SuppressWarnings("unchecked")
	public Collection<Questions> listQuestions(String filter, String value) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Questions.class);
		
		if(filter.equals("") && value.equals("")){
			criteria.addOrder(Order.desc("que_id"));
		}else if(filter.equals("id")){
			criteria.add(Restrictions.sqlRestriction(" que_id LIKE '%"+value+"%' ")).addOrder(Order.desc("que_id"));
		}else if(filter.equals("topic")){
			criteria = session.createCriteria(Questions.class,"q");
			criteria.createCriteria("topic", "t"); //Questions class should have a object called 'topic' 
			criteria.add(Restrictions.like("t.title", "%"+value+"%")).addOrder(Order.asc("t.title"));
			
		}else{//description
			criteria.add(Restrictions.like("description", "%"+value+"%")).addOrder(Order.asc("description"));
		}
		
		return (Collection<Questions>) criteria.list();	
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Questions> listQuestionsByProfiles(String where,
			Integer level) {
		return sessionFactory.getCurrentSession().createQuery("FROM Questions WHERE topic IN("+where+") AND level = "+level).list();
	}
	
	public List<Items> listItems(Integer id) {
		Questions question = getQuestion(id);
		return question.getItemsList();
	}

	public void removeQuestion(Integer id) {
		Questions question = getQuestion(id);
		if(question != null){
			sessionFactory.getCurrentSession().delete(question);
		}
	}

	public void removeItem(Integer ite_id) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete((Items) getItem(ite_id));
		session.getTransaction().commit();
		session.close();
	}
	
	public void removeItem(Integer que_id, Integer ite_id) {
		Questions question = getQuestion(que_id);
		List <Items> list = question.getItemsList();
		Items i = new Items();
		i.setId(ite_id);
		list.remove(i);
		question.setItemsList(list);
		updateQuestion(question);
	}

	public void updateQuestion(Questions question) {
		sessionFactory.getCurrentSession().update(question);
	}
	

	public void updateItem(Integer que_id, Items item) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		item.setQuestion(getQuestion(que_id));
		session.update(item);
		session.getTransaction().commit();
		session.close();
	}

	public Questions getQuestion(Integer id) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Questions question = (Questions) session.get(Questions.class, id);
		Hibernate.initialize(question.getItemsList()); 
		Hibernate.initialize(question.getTopic()); 
		session.getTransaction().commit();
		session.close();
		
		return question;
	}

	public Items getItem(Integer id) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Items item = (Items) session.get(Items.class, id);
		session.getTransaction().commit();
		session.close();
		
		
		return item;
	}


}
